precision mediump float;

varying vec4 v_position;


vec4 pack(float depth) {

const vec4 bitSh = vec4(256.0 * 256.0 * 256.0,
						256.0 * 256.0, 
						256.0, 
						1.0);
const vec4 bitMsk = vec4(0, 1.0 / 256.0, 1.0 / 256.0, 1.0 / 256.0);
vec4 comp = fract(depth * bitSh);
comp -= comp.xxyz * bitMsk;
return comp;
}

void main(){

// distance entspricht der Tiefe des Vertex in Lichtkoordinaten - "r/q"
float distance = v_position.z / v_position.w;

// scale form 0 to 1
distance = (distance + 1.0) / 2.0;

// bias to prevent artifacts
distance += 0.0005;


//gl_FragColor = vec4(0.0, 0.0, 0.0, distance);
gl_FragColor = pack(distance);

}
